package org.zju.demo;

import com.google.gson.Gson;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.zju.demo.model.request.ReqeustSetOrderStatusJson;
import org.zju.demo.utils.Constant;


/**
 * Created by ZJDX on 2015/8/30.
 */
public class testUserAPI {
    private static final String BASE_URL="http://10.214.143.25:8080";
    private static void frame(String url, JSONObject jsonObject) {

        HttpClient httpClient = HttpClientBuilder.create().build();

        HttpPost post = new HttpPost(url);
        post.setHeader("Content-type", "application/json");
        post.setEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
        System.out.println(jsonObject.toString());
        try {
            HttpResponse response = httpClient.execute(post);
            System.out.println("response Code" + response.getStatusLine().getStatusCode());
            if (response.getStatusLine().getStatusCode() == 200) {
                String retSrc = EntityUtils.toString(response.getEntity());
                JSONObject json = new JSONObject(retSrc);
                System.out.println(json);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void testLogin() {
        JSONObject jsonObject = new JSONObject();
        String url = BASE_URL + "/api/user/login";
        System.out.println("testLogin begin");
        try{
            jsonObject.put("command_id", 0x01);
            jsonObject.put("username", "ca");
            jsonObject.put("password", "1234567");
            frame(url, jsonObject);
        }catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("testLogin end");
    }
    public static void testHot() {
        JSONObject jsonObject = new JSONObject();
        String url = BASE_URL + "/api/shopping/getHot";
        try{
            jsonObject.put("command_id", 0x16);
            frame(url, jsonObject);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void testProduce() {
        JSONObject jsonObject = new JSONObject();
        String url = BASE_URL + "/api/order/produce";
        url = "http://10.214.143.25:8080//api/order/produce";
        try{
            jsonObject.put("command_id", 0x17);
            jsonObject.put("username", "admin");
            frame(url, jsonObject);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void testSetOrderStatus() {
        String url = BASE_URL + "/api/order/setOrderStatus";
        try{
            ReqeustSetOrderStatusJson json = new ReqeustSetOrderStatusJson();
            json.setCommand_id(0x15);
            json.setUsername("admin");
            json.setOrder_id(14);
            json.setOrder_status(Constant.ORDER_STATUS_BEFORE_PRODUCE);

            String tmp = new Gson().toJson(json);
            JSONObject jsonObject = new JSONObject(tmp);
            frame(url, jsonObject);
        }catch ( Exception e) {
        }
    }
    public static void main(String[] args) {

//        testHot();
//        testProduce();
        testSetOrderStatus();
    }

}
